package me.nereo.muti_image_selector.dialog;

import java.util.List;

import me.nereo.multi_image_selector.MultiImageSelectorFragment.OnFolderChangeListener;
import me.nereo.multi_image_selector.R;
import me.nereo.multi_image_selector.adapter.FolderAdapter;
import me.nereo.multi_image_selector.bean.Folder;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.PopupWindow;

public class FoldPopupWindow extends PopupWindow{
	private View rootView;
	private Context mContext;
	private ListView mLvFolders;
	private FolderAdapter mFolderAdapter;
	private OnFolderChangeListener mOnFolderChangeListener;
	public FoldPopupWindow(Context context) {
		super(context);
		rootView = LayoutInflater.from(context)
				.inflate(R.layout.layout_pop_folders, null);
		this.mContext = context;
		setFocusable(true);
		setOutsideTouchable(true);
		setWidth(LayoutParams.MATCH_PARENT);
		setHeight(LayoutParams.MATCH_PARENT);
		setContentView(rootView);
		setBackgroundDrawable(new BitmapDrawable());
		bindView();
		bindEvent();
	}
	private void bindEvent() {
		mFolderAdapter = new FolderAdapter(mContext);
		mLvFolders.setAdapter(mFolderAdapter);
		mLvFolders.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				mFolderAdapter.setSelectIndex(position);
				if (mOnFolderChangeListener != null) {
					if (position == 0) {
						mOnFolderChangeListener.onFolderChanged("全部图片",null,position);
					} else {
						mOnFolderChangeListener
								.onFolderChanged(((Folder) mFolderAdapter.getItem(position)).name,(Folder) mFolderAdapter.getItem(position),position);
					}
				}
				dismiss();
			}
			
		});
	}
	private void bindView() {
		mLvFolders = (ListView) rootView.findViewById(R.id.lv_folders);		
	}
	
	public void setData(List<Folder> folders){
		mFolderAdapter.setData(folders);
	}
	
	public int getSelectIndex(){
		return mFolderAdapter.getSelectIndex();
	}
	
	public void setSelectIndex(int index){
		mFolderAdapter.setSelectIndex(index);
	}
	
	public void show(View v){
		showAsDropDown(v);
	}
	
	public void setOnFolderChangeListener(OnFolderChangeListener onFolderChangeListener){
		this.mOnFolderChangeListener = onFolderChangeListener;
	}
	
}
